package com.loong.shop.controller.backend;

import com.loong.common.mvc.BaseBackendController;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.shop.common.backend.param.GoodsSkuParam;
import com.loong.shop.common.backend.param.GoodsSkuQueryParam;
import com.loong.shop.handler.GoodsSkuHandler;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 处理后台商品sku请求的Controller
 * @Author: 刘群
 * @Date: 2020/10/27 8:55
 */
@RestController
@RequestMapping("manage/shop/goodsSku")
public class GoodsSkuController extends BaseBackendController {

    @Resource
    private GoodsSkuHandler skuHandler;

    /**
     * 新增商品sku
     * @param param 商品sku封装的参数
     * @return 响应的结果
     */
    @PostMapping("save")
    public Result saveGoodsSku(@Validated({CommonInsert.class}) GoodsSkuParam param){
        param.setId(null);
        return handlerExecutor.execute(() -> skuHandler.saveGoodsSku(param));
    }

    /**
     * 更新商品sku
     * @param param 商品sku封装的参数
     * @return 响应的结果
     */
    @PostMapping("update")
    public Result updateGoodsSku(@Validated({CommonUpdate.class}) GoodsSkuParam param){
        return handlerExecutor.execute(() -> skuHandler.updateGoodsSku(param));
    }

    /**
     * 删除商品sku
     * @param idList 商品sku的id集合
     * @return 响应的结果
     */
    @PostMapping("delete")
    public Result deleteGoodsSku(@RequestParam("idList") List<Long> idList){
        if (CollectionUtils.isEmpty(idList)) return null;
        return handlerExecutor.execute(() -> skuHandler.deleteGoodsSku(idList));
    }

    /**
     * 分页查询商品sku
     * @param param 查询参数
     * @return 响应的结果
     */
    @PostMapping("pageQuery")
    public Result pageQueryGoodsSku(GoodsSkuQueryParam param){
        return handlerExecutor.execute(() -> skuHandler.pageQueryGoodsSku(param));
    }
}
